﻿using NHibernate;
using NHibernate.Criterion;
using SmartRS.com.vn.smartrs.domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartRS.com.vn.smartrs.dao
{
    public class BillDao : BaseDao<Bill>
    {
        private static BillDao _billDao = new BillDao();
        private BillDao()
        { }

        public static BillDao getInstance()
        {
            return _billDao;
        }

        public Bill getBillByDeskId(long deskId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                try
                {
                    var result = session.QueryOver<Bill>().Where(x => x.desk.deskId == deskId && x.deteleFlag == false).List<Bill>();
                    if (result != null && result.Count > 0)
                    {
                        return result[0];
                    }

                    return new Bill();
                }
                catch (Exception ex)
                {
                    throw new Exception("getBillByDeskId happened error: " + ex.Message);
                }
            }
        }

        public IList<Bill> getListBillAsDate(DateTime startTime, DateTime endTime)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                try
                {
                    var result = session.QueryOver<Bill>().Where(x => x.outputDate > startTime && x.outputDate < endTime && x.deteleFlag == false).List<Bill>();

                    return (result != null) ? result : new List<Bill>();
                }
                catch (Exception ex)
                {
                    throw new Exception("getListBillAsDate happened error: " + ex.Message);
                }
            }
        }

        public IList<Bill> getListBillAsMonth(DateTime date)
        {
            try
            {
                DateTime startTime = date; //dummy date -> move first day of month
                DateTime endTime = date; //next month -> move first day of next month

                IList<Bill> listBill = getListBillAsDate(startTime, endTime);

                return (listBill != null) ? listBill : new List<Bill>();
            }
            catch (Exception ex)
            {
                throw new Exception("getListBillAsMonth happened error: " + ex.Message);
            }
        }

        public IList<Bill> getListBillBySerialAndDate(string serial, DateTime startdDate, DateTime endDate, bool isUseDate)
        {
            try
            {
                using (ISession session = NHibernateHelper.OpenSession())
                {
                    TimeSpan start = new TimeSpan(0, 0, 0);
                    TimeSpan end = new TimeSpan(23, 59, 59);
                    startdDate = startdDate.Date + start;
                    endDate = endDate.Date + end;

                    var result = session.QueryOver<Bill>().Where(Restrictions.On<Bill>(c => c.billSerial).IsLike("%"+serial.Trim()+"%")).List<Bill>();
                    result = isUseDate ? result.Where(x => x.outputDate >= startdDate).ToList<Bill>() : result;
                    result = isUseDate ? result.Where(x => x.outputDate < endDate).ToList<Bill>() : result;

                    return (result != null) ? result : new List<Bill>();

                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
